package com.gouda.core.service.impl.merchantManager;

import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.gouda.core.dao.MerchantDao;
import com.gouda.core.dto.merchantManage.MerchantDto;
import com.gouda.core.entity.MerchantEntity;
import com.gouda.core.enums.EnumAuditState;
import com.gouda.core.enums.EnumRegisterStep;
import com.gouda.core.service.BaseService;
import com.gouda.core.service.merchantManage.MerchantManageService;
import com.gouda.core.service.merchantManage.MerchantWalletService;
import com.gouda.core.service.systemManage.MerchantUserService;
import com.hephaestus.utils.DataUtil;
import com.themis.constant.FrontConstant;
import org.amethyst.atlas.basic.component.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by KGL on 2015/12/5.
 */
@Service
public class MerchantManagerServiceImpl extends BaseService implements MerchantManageService {

    @Autowired
    private MerchantDao merchantDao;
    @Autowired
    private MerchantUserService merchantUserService;
    @Autowired
    private MerchantWalletService merchantWalletService;

    @Override
    public boolean addMerchant(final MerchantDto merchantDto, final String loginUserId) {
        boolean operateResult = true;
        MerchantEntity entity = ENTITY_FACTORY.generateDBEntity(MerchantEntity.class);
        entity.setIncCode(merchantDto.getIncCode());
        entity.setIncName(merchantDto.getIncName());
        entity.setState(EnumAuditState.AUDITING.getCode());
        entity.setAddress(merchantDto.getAddress());
        entity.setContacts(merchantDto.getContacts());
        entity.setContactsPhone(merchantDto.getContactsPhone());
        entity.setHousePid(merchantDto.getHousePid());
        entity.setIdCodePid(merchantDto.getIdCodePid());
        entity.setLicensePid(merchantDto.getLicensePid());
        entity.setCreateTime(new Date());
        entity.setRegisterStep(EnumRegisterStep.FILL_MERCHANT_BANK_INFO.getCode());
        boolean createFlag = merchantDao.insert(entity);
        //auth merchant id to merchant admin when merchant is created
        if (createFlag){
            operateResult = merchantUserService.authMerchant2Admin(entity.getId(), loginUserId);
        }
        //create merchant wallet
        if (operateResult){
            operateResult = merchantWalletService.createMerchantWallet(entity.getId());
        }
        return operateResult;
    }

    @Override
    public boolean modifyMerchant(MerchantDto merchantDto) {
        //TODO based on concrete business require
        final long id = merchantDto.getId();
        MerchantEntity entity = merchantDao.unique(MerchantEntity.class, id);


        entity.setState(EnumAuditState.AUDITING.getCode());
        return merchantDao.update(entity);
    }

    @Override
    public PageData<MerchantDto> getMerchantsByPage(final String incCode, final String incName, final String state, final String pageNum) {

        final int count = merchantDao.seekCountByConditions(incCode, incName, state);
        if (count <= 0) {
            return null;
        }
        int queryPageNum = FrontConstant.DEFAULT_START_PAGE;
        if (!Strings.isNullOrEmpty(pageNum)) {
            queryPageNum = Integer.valueOf(pageNum);
        }
        PageData<MerchantDto> pageData = new PageData<>(count, queryPageNum, FrontConstant.DEFAULT_PAGE_SIZE);
        List<MerchantEntity> merchantList = merchantDao.seekMerchantsByConditions(incCode, incName, state, pageData.getSqlStart(), pageData.getRowsPerPage());
        pageData.setData(this.generateDtos(merchantList));
        return pageData;
    }

    @Override
    public boolean auditMerchant(final long merchantId, final int state) {
        MerchantEntity entity = merchantDao.unique(MerchantEntity.class, merchantId);
        entity.setState(state);
        return merchantDao.update(entity);
    }

    private List<MerchantDto> generateDtos(final List<MerchantEntity> merchantList){
        List<MerchantDto> dtos = Lists.newArrayList();
        for (MerchantEntity merchantEntity : merchantList) {
            MerchantDto dto = new MerchantDto();
            dto.setId(merchantEntity.getId());
            dto.setState(merchantEntity.getState());
            dto.setAddress(merchantEntity.getAddress());
            dto.setContacts(merchantEntity.getContacts());
            dto.setContactsPhone(merchantEntity.getContactsPhone());
            dto.setHousePid(merchantEntity.getHousePid());
            dto.setIdCodePid(merchantEntity.getIdCodePid());
            dto.setIncCode(merchantEntity.getIncCode());
            dto.setIncName(merchantEntity.getIncName());
            dto.setLicensePid(merchantEntity.getLicensePid());
            final int state = merchantEntity.getState();
            dto.setState(state);
            dto.setStateDesc(EnumAuditState.fromInteger(state).getMsg());
            dto.setCreateTime(DataUtil.formatDate(merchantEntity.getCreateTime()));
            dtos.add(dto);
        }
        return dtos;
    }

}
